/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pp.persistance;

import com.pp.entites.Evenement;
import com.pp.entites.EvenementPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.pp.entites.Personnel;
import com.pp.persistance.exceptions.NonexistentEntityException;
import com.pp.persistance.exceptions.PreexistingEntityException;
import com.pp.persistance.exceptions.RollbackFailureException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author arnaud
 */
public class EvenementJpaController implements Serializable {

    public EvenementJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Evenement evenement) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (evenement.getEvenementPK() == null) {
            evenement.setEvenementPK(new EvenementPK());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Personnel idpersonnel = evenement.getIdpersonnel();
            if (idpersonnel != null) {
                idpersonnel = em.getReference(idpersonnel.getClass(), idpersonnel.getId());
                evenement.setIdpersonnel(idpersonnel);
            }
            em.persist(evenement);
            if (idpersonnel != null) {
                idpersonnel.getEvenementList().add(evenement);
                idpersonnel = em.merge(idpersonnel);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findEvenement(evenement.getEvenementPK()) != null) {
                throw new PreexistingEntityException("Evenement " + evenement + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Evenement evenement) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Evenement persistentEvenement = em.find(Evenement.class, evenement.getEvenementPK());
            Personnel idpersonnelOld = persistentEvenement.getIdpersonnel();
            Personnel idpersonnelNew = evenement.getIdpersonnel();
            if (idpersonnelNew != null) {
                idpersonnelNew = em.getReference(idpersonnelNew.getClass(), idpersonnelNew.getId());
                evenement.setIdpersonnel(idpersonnelNew);
            }
            evenement = em.merge(evenement);
            if (idpersonnelOld != null && !idpersonnelOld.equals(idpersonnelNew)) {
                idpersonnelOld.getEvenementList().remove(evenement);
                idpersonnelOld = em.merge(idpersonnelOld);
            }
            if (idpersonnelNew != null && !idpersonnelNew.equals(idpersonnelOld)) {
                idpersonnelNew.getEvenementList().add(evenement);
                idpersonnelNew = em.merge(idpersonnelNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                EvenementPK id = evenement.getEvenementPK();
                if (findEvenement(id) == null) {
                    throw new NonexistentEntityException("The evenement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(EvenementPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Evenement evenement;
            try {
                evenement = em.getReference(Evenement.class, id);
                evenement.getEvenementPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The evenement with id " + id + " no longer exists.", enfe);
            }
            Personnel idpersonnel = evenement.getIdpersonnel();
            if (idpersonnel != null) {
                idpersonnel.getEvenementList().remove(evenement);
                idpersonnel = em.merge(idpersonnel);
            }
            em.remove(evenement);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Evenement> findEvenementEntities() {
        return findEvenementEntities(true, -1, -1);
    }

    public List<Evenement> findEvenementEntities(int maxResults, int firstResult) {
        return findEvenementEntities(false, maxResults, firstResult);
    }

    private List<Evenement> findEvenementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Evenement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Evenement findEvenement(EvenementPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Evenement.class, id);
        } finally {
            em.close();
        }
    }

    public int getEvenementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Evenement> rt = cq.from(Evenement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
